package com.well.register.console.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.well.register.console.aop.WellException;
import com.well.register.console.dao.JobParamMapper;
import com.well.register.console.domain.JobParam;
import com.well.register.console.domain.JobParamHis;
import com.well.register.console.model.job.UpdateJobParam;
import com.well.register.console.service.JobParamHisService;
import com.well.register.console.service.JobParamService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.well.register.console.model.job.ErrorInfo.INVALID_PARAM_ID;
import static com.well.register.console.model.job.ErrorInfo.PARAM_KEY_EXIST;


/**
 * <p>
 * job service impl
 * </p>
 *
 * @author weedo
 * @since 2022/8/7
 */

@Service
public class JobParamServiceImpl extends ServiceImpl<JobParamMapper, JobParam> implements JobParamService {

    @Resource
    private JobParamHisService jobParamHisService;

    @Override
    public boolean saveBatchParam(List<JobParam> jobParams) {
        return this.saveBatch(jobParams);
    }

    @Override
    public List<JobParam> getJobParams(Integer jobId) {
        QueryWrapper<JobParam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("job_id", jobId);
        return this.list(queryWrapper);
    }

    public JobParam findJobParamByParamKey(String paramKey, Integer jobId) {
        QueryWrapper<JobParam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("param_key", paramKey);
        queryWrapper.eq("job_id", jobId);
        return this.getOne(queryWrapper);
    }

    @Override
    public boolean saveJobParam(JobParam param) {
        JobParam jobParam = findJobParamByParamKey(param.getParamKey(), param.getJobId());
        if (jobParam != null) {
            throw new WellException(PARAM_KEY_EXIST.getCode(),
                    String.format(PARAM_KEY_EXIST.getMsg(), param.getParamKey()));
        }
        JobParamHis his = jobParamHisService.getMaxVersionHisByParamKey(param.getParamKey());
        if (his != null) {
            param.setVersion(his.getVersion() + 1);
        }
        return this.save(param);
    }

    @Override
    public boolean deleteJobParam(Integer jobParamId) {
        JobParam param = this.getById(jobParamId);
        if (param == null) {
            throw new WellException(INVALID_PARAM_ID.getCode(),
                    String.format(INVALID_PARAM_ID.getMsg(), param.getParamKey()));
        }
        JobParamHis jobParamHis = convert2ParamHis(param);
        jobParamHisService.save(jobParamHis);
        return this.removeById(jobParamId);
    }

    @Override
    public boolean updateJobParam(UpdateJobParam param) {
        JobParam jobParam = findJobParamByParamKey(param.getParamKey(), param.getJobId());
        JobParamHis jobParamHis = convert2ParamHis(jobParam);
        jobParamHisService.save(jobParamHis);
        jobParam.setVersion(jobParam.getVersion() + 1);
        jobParam.setParamValue(param.getParamValue());
        return this.updateById(jobParam);
    }

    private JobParamHis convert2ParamHis(JobParam param) {
        JobParamHis his = new JobParamHis();
        BeanUtils.copyProperties(param, his, "id");
        his.setCreateTime(new Date());
        return his;
    }
}
